package engine.display
{
	/**
	 * 此类用与管理对话时，人物头上的气泡显示
	 * @autho wanghaibo 
	 * 单例模式
	 */	
	import engine.data.ModelLocator;
	import engine.data.SceneObject;
	import engine.flag.GameDefine;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import newx.com.config.StaticIni;

	public class TalkManage
	{
		private  var _model:ModelLocator;
		private  var _playArray:Array;
		private  var _npcArray:Array;
		private static var talkBoxs : Array = [];
		
		private static var _self		:TalkManage;
		public static function getInstance():TalkManage
		{
			if(_self == null)
			{
				_self	=	new TalkManage();
				
			}
			return _self
		}
		
		public function TalkManage()
		{
			_playArray	=	[];
			_npcArray = [];
			_model		=	ModelLocator.getInstance()
		}
		
		private static function getFreeTalkBox():TalkBox
		{
			for each( var talkBox :TalkBox in talkBoxs )
			{
				if(talkBox.isUse)
				{
					return talkBox;
				}
			}
			
			var tTalkBox : TalkBox = new TalkBox();
			talkBoxs.push( tTalkBox);
			return tTalkBox;
		}
		
		/**
		 * 
		 * @param val
		 * 在场景上添加聊天时的对话信息 
		 */		
		public function setTalk(val:Object):void
		{
			var channelval	:int	=	val[0];
			
			if(channelval	==	GameDefine.CHATTYPE_WHISPER_ECHO)
			{
				return
				//				tempName	=	_model.scene.Role.GetPropertiesEx("Name")
			}
			
			if(channelval != GameDefine.CHATTYPE_GM_HELP && channelval != GameDefine.CHATTYPE_TOGM)
			{
				var tempName:String	=	val[1];
				var tempTalk:String	=	val[2];
				
				if(channelval == GameDefine.CHATTYPE_WORLD)
				{
					tempTalk = tempTalk.substr(0, tempTalk.length -1)
				}
				setTalkContent(tempName,tempTalk);
			}
			
		}
		
		/**
		 * 设定聊天显示内容，根据对象名 
		 * @param npcName
		 * @param talkContent
		 * 
		 */		
		public function setTalkContent(npcName:String,talkContent:String):void
		{
//			talkContent = talkContent.substr(0, talkContent.length -1);
			for each(var tempData:Array in _playArray)
			{
				if(tempData[1] == npcName)
				{
					//显示对象
					var tempMc		:Sprite		=	tempData[0];
					var tempWidht	:Number		=	tempData[2];
					var tempHeight	:Number		=	tempData[3];
					var tempWar		:Boolean	=	tempData[4];
					var tempNpcObj	:SceneObject=	tempData[5];
					if(tempMc.visible){
						var tempTalkBox:TalkBox	=	getFreeTalkBox();
						tempTalkBox.setTalk(talkContent);
						//当人物有缩放时，反计算出文字相应的放大数
						var tempScal:Number		=	1/tempMc.scaleX;
						tempTalkBox.scaleX	=	tempTalkBox.scaleY	=	tempScal;
						var tempX:Number	=	tempWidht - tempTalkBox.width;	
						tempTalkBox.x	=	-tempWidht/2	+ tempX/2
						tempTalkBox.name	=	"TalkBox";
						//根据不同的状态来设定显示坐标

//						if(tempWar){
//							var childMC:DisplayObject	=	tempMc.getChildAt(0)
//							tempTalkBox.y	=	- childMC.height	-tempTalkBox.height- 25
//								
//						}else{
//							tempTalkBox.y	=	-tempHeight*tempScal -tempTalkBox.height -25;
//						}
						var tempY:Number	=	0;
						for(var i: int = 0; i < tempMc.numChildren; i++)
						{
							var checkMc:DisplayObject	=	tempMc.getChildAt(i);
							if(checkMc.name	!=	"TalkBox")
							{
								tempY	=	Math.min(checkMc.y -checkMc.height , tempY);
							}
						}
						
						tempTalkBox.y	=	tempY - tempTalkBox.height;
						var tempCheckBox:TalkBox	=	tempMc.getChildByName("TalkBox") as TalkBox
						if(tempCheckBox != null){
							tempCheckBox.removeTalkBox();
						}
						tempMc.addChild(tempTalkBox);
					}
				}
			}
		}
		
		private var timer:Timer;
		public function allshowNPCTalkContent():void
		{
			if(timer!=null)
			{
				timer.removeEventListener(TimerEvent.TIMER,npcTalkTimer);
				timer.stop();
				timer = null;
			}
			
			timer = new Timer(5000);
			timer.addEventListener(TimerEvent.TIMER,npcTalkTimer);
			timer.start();
		}
		
		public function npcTalkTimer(e:TimerEvent):void
		{
			for each(var tempData:Array in _npcArray)
			{
				if(StaticIni.getSwfLangStr("NpcTalk_" + tempData[1]) == "NpcTalk_" + tempData[1] )
					continue;
				//显示对象
				var tempMc		:Sprite		=	tempData[0];
				var tempWidht	:Number		=	tempData[2];
				var tempHeight	:Number		=	tempData[3];
				var tempWar		:Boolean	=	tempData[4];
				var tempNpcObj	:SceneObject=	tempData[5];
				if(tempMc.visible)
				{
					var tempTalkBox:TalkBox	=	getFreeTalkBox();
					tempTalkBox.setTalk(StaticIni.getSwfLangStr("NpcTalk_"+tempData[1]));

					//当人物有缩放时，反计算出文字相应的放大数
					var tempScal:Number		=	1/tempMc.scaleX;
					tempTalkBox.scaleX	=	tempTalkBox.scaleY	=	tempScal;
					var tempX:Number	=	tempWidht - tempTalkBox.width;	
					tempTalkBox.x	=	-tempWidht/2	+ tempX/2
					tempTalkBox.name	=	"TalkBox";
					//根据不同的状态来设定显示坐标
					
					//						if(tempWar){
					//							var childMC:DisplayObject	=	tempMc.getChildAt(0)
					//							tempTalkBox.y	=	- childMC.height	-tempTalkBox.height- 25
					//								
					//						}else{
					//							tempTalkBox.y	=	-tempHeight*tempScal -tempTalkBox.height -25;
					//						}
					var tempY:Number	=	0;
					for(var i: int = 0; i < tempMc.numChildren; i++)
					{
						var checkMc:DisplayObject	=	tempMc.getChildAt(i);
						if(checkMc.name	!=	"TalkBox")
						{
							tempY	=	Math.min(checkMc.y -checkMc.height , tempY);
						}
					}
					
//					tempTalkBox.y	=	tempY - tempTalkBox.height;
					tempTalkBox.y	=	-215;
					var tempCheckBox:TalkBox	=	tempMc.getChildByName("TalkBox") as TalkBox
					if(tempCheckBox != null)
					{
						tempCheckBox.removeTalkBox();
					}
					tempMc.addChild(tempTalkBox);
				}
			}
		}
		
		/**
		 * 设定NPC聊天显示内容，根据对象名 
		 * @param npcName
		 * @param talkContent
		 * 
		 */		
		public function setNPCTalkContent(npcName:String,talkContent:String):void
		{
			//			talkContent = talkContent.substr(0, talkContent.length -1);
			for each(var tempData:Array in _npcArray)
			{
				if(tempData[1] == npcName)
				{
					//显示对象
					var tempMc		:Sprite		=	tempData[0];
					var tempWidht	:Number		=	tempData[2];
					var tempHeight	:Number		=	tempData[3];
					var tempWar		:Boolean	=	tempData[4];
					var tempNpcObj	:SceneObject=	tempData[5];
					if(tempMc.visible){
						var tempTalkBox:TalkBox	=	getFreeTalkBox();
						tempTalkBox.setTalk(talkContent);
						//当人物有缩放时，反计算出文字相应的放大数
						var tempScal:Number		=	1/tempMc.scaleX;
						tempTalkBox.scaleX	=	tempTalkBox.scaleY	=	tempScal;
						var tempX:Number	=	tempWidht - tempTalkBox.width;	
						tempTalkBox.x	=	-tempWidht/2	+ tempX/2
						tempTalkBox.name	=	"TalkBox";
						//根据不同的状态来设定显示坐标
						
						//						if(tempWar){
						//							var childMC:DisplayObject	=	tempMc.getChildAt(0)
						//							tempTalkBox.y	=	- childMC.height	-tempTalkBox.height- 25
						//								
						//						}else{
						//							tempTalkBox.y	=	-tempHeight*tempScal -tempTalkBox.height -25;
						//						}
						var tempY:Number	=	0;
						for(var i: int = 0; i < tempMc.numChildren; i++)
						{
							var checkMc:DisplayObject	=	tempMc.getChildAt(i);
							if(checkMc.name	!=	"TalkBox")
							{
								tempY	=	Math.min(checkMc.y -checkMc.height , tempY);
							}
						}
						
						tempTalkBox.y	=	tempY - tempTalkBox.height;
						var tempCheckBox:TalkBox	=	tempMc.getChildByName("TalkBox") as TalkBox
						if(tempCheckBox != null){
							tempCheckBox.removeTalkBox();
						}
						tempMc.addChild(tempTalkBox);
					}
				}
			}
		}
		
		
		/**
		 * 将所有需要显示的人物添加进数组，方便管理
		 * @param val 所要添加对话内容的对象
		 * @param name 对象名
		 * @param width 
		 * @param height
		 * @param isWar 是否为战斗装态下的人物
		 * 
		 */		
		public function addPlay(val:Sprite, name:String, width:Number,height:Number, isWar:Boolean,sendData:SceneObject):void
		{
			for(var i:int =0; i <_playArray.length; i++)
			{
				var tempCheck:Array	=	_playArray[i];
				if(name == tempCheck[1])
				{
					return;
				}
			}
			_playArray.push([val,name,width,height,isWar,sendData]);
		}
		
		/**
		 * 将所有需要显示的人物添加进数组，方便管理
		 * @param val 所要添加对话内容的对象
		 * @param name 对象名
		 * @param width 
		 * @param height
		 * @param isWar 是否为战斗装态下的人物
		 * 
		 */	
		public function addNPC(val:Sprite, name:String, width:Number,height:Number, isWar:Boolean,sendData:SceneObject):void
		{
			for(var i:int =0; i <_npcArray.length; i++)
			{
				var tempCheck:Array	=	_npcArray[i];
				if(name == tempCheck[1])
				{
					return;
				}
			}
			_npcArray.push([val,name,width,height,isWar,sendData]);
			
		}
		/**
		 * 根据人物名的索引来删除对象 
		 * @param name
		 * 
		 */		
		public function removePlay(name:String):void
		{
			for(var i:int =0; i< _playArray.length; i++)
			{
				if(_playArray[i][1]	== name){
					_playArray.splice(i,1);
				}
			}
			
			for(var j:int =0; j< _npcArray.length; j++)
			{
				if(_npcArray[j][1]	== name){
					_npcArray.splice(j,1);
				}
			}
		}
		
		/**
		 * 清空管理数组 
		 * 场景切换时会调用
		 */		
		public function clearArray():void
		{
			_playArray	=	[];
			_npcArray  =   [];
		}
	}
}